package cn.edu.nankai.bookstore.controller.order;

import cn.edu.nankai.bookstore.domain.po.Customer;
import cn.edu.nankai.bookstore.domain.po.Order;
import cn.edu.nankai.bookstore.utils.Services;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "GetUserOrdersServlet", value = "/GetUserOrdersServlet")
public class GetUserOrdersServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("content-type","text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        Customer customer = (Customer) session.getAttribute("user");
        if(customer == null) return;
        List<Order> orders = Services.orderService.getByCustomerId(customer.getCustomerId());
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(orders);
        response.getWriter().write(json);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
